package cn.itcast.controller;

import cn.itcast.domain.*;
import cn.itcast.service.AreaService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin
@Controller
@RequestMapping("/area")
public class AreaController {

  @Autowired
  private AreaService areaService;
  private String jsonArea;


  /**
   * 查找城市区域房价分布
   *
   * @param city 城市名
   * @param request 请求
   * @return 地区房价表
   */
  @ResponseBody
  @RequestMapping(value = "/priceArea", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
  public String PriceByArea(String city, HttpServletRequest request) {

    Map<String, Object> map = new HashMap<>();
    List<PriceArea> areas = areaService.findAreaPrice(city);
    HttpSession session = request.getSession(true);

    if (!areas.isEmpty()){
      session.setAttribute("areas", areas);
      map.put("status",0);
      map.put("message","查找成功");
      map.put("areas",areas);
    }else{
      map.put("status",1);
      map.put("message","没有该城市");
    }
    jsonArea = JSON.toJSONString(map);
    return jsonArea;

  }


  /**
   * 查找地区房价随时间的走势
   *
   * @param area 地区名
   * @param request 请求
   * @return 地区房价走势表
   */
  @ResponseBody
  @RequestMapping(value = "/priceTrend", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
  public String TrendByPrice(String area, HttpServletRequest request){
    Map<String, Object> map = new HashMap<>();
    List<PriceTrend> trends = areaService.findTrendPrice(area);
    HttpSession session = request.getSession(true);

    if (!trends.isEmpty()){
      session.setAttribute("trends", trends);
      map.put("status",0);
      map.put("message","查找成功");
      map.put("trends",trends);
    }else{
      map.put("status",1);
      map.put("message","没有房价走势");
    }
    jsonArea = JSON.toJSONString(map);
    return jsonArea;

  }


  /**
   * 根据房价寻找地区
   *
   * @param minprice 最小价格
   * @param maxprice  最高价格
   * @param request 请求
   * @return 城市地区房价
   */
  @ResponseBody
  @RequestMapping(value = "/priceRange", method = RequestMethod.POST,  produces = "text/plain;charset=utf-8")
  public String AreaByPriceRange(int minprice, int maxprice, HttpServletRequest request){
    Map<String, Object> map = new HashMap<>();
    List<PriceRange> Ranges = areaService.findRangePrice(minprice, maxprice);
    HttpSession session = request.getSession(true);

    if (!Ranges.isEmpty()){
      session.setAttribute("Ranges", Ranges);
      map.put("status",0);
      map.put("message","查找成功");
      map.put("Ranges",Ranges);
    }else{
      map.put("status",1);
      map.put("message","区间没有值");
    }
    jsonArea = JSON.toJSONString(map);
    return jsonArea;
  }


}
